//
//  MessageViewController.m
//  项目框架 - by Candy
//
//  Created by Candy on 17/1/5.
//  Copyright © 2017年 Candy. All rights reserved.
//

#import "MessageViewController.h"

@interface MessageViewController ()

@property (nonatomic, strong) NSMutableArray    *dataSource;

@end

@implementation MessageViewController

- (void)viewDidDisappear:(BOOL)animated {
    [self.player stop];
}

- (void)viewWillAppear:(BOOL)animated {
    [self.player play];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    self.navigationController.navigationBar.hidden = YES;
//    self.tabBarController.tabBar.hidden = YES;
    self.player.backgroundPlayEnable = YES;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    self.view.frame = CGRectMake(0, 0, 100, 150);
    
    PLPlayerOption *option = [PLPlayerOption defaultOption];
    [option setOptionValue:@15 forKey:PLPlayerOptionKeyTimeoutIntervalForMediaPackets];
    //rtmp://live.hkstv.hk.lxdns.com/live/hks
    //rtmp://120.24.58.29:1935/live/743c392125b1edb2e726a2f07cf2d455
    //rtmp://v1.one-tv.com/live/mpegts.stream
    //rtmp://live.hkstv.hk.lxdns.com/live/hks

    NSArray *tvArray = [self.dataSource[42] componentsSeparatedByString:@","];
    NSURL *url = [NSURL URLWithString:tvArray[1]];
    
    self.player = [PLPlayer playerWithURL:url option:option];
    self.player.delegate = self;
    
    [self.view addSubview:self.player.playerView];
    
    [self.player play];

}

// 实现 <PLPlayerDelegate> 来控制流状态的变更
- (void)player:(nonnull PLPlayer *)player statusDidChange:(PLPlayerStatus)state {
    // 这里会返回流的各种状态，你可以根据状态做 UI 定制及各类其他业务操作
    // 除了 Error 状态，其他状态都会回调这个方法
    // 开始播放，当连接成功后，将收到第一个 PLPlayerStatusCaching 状态
    // 第一帧渲染后，将收到第一个 PLPlayerStatusPlaying 状态
    // 播放过程中出现卡顿时，将收到 PLPlayerStatusCaching 状态
    // 卡顿结束后，将收到 PLPlayerStatusPlaying 状态
}


- (void)player:(nonnull PLPlayer *)player stoppedWithError:(nullable NSError *)error {
    // 当发生错误，停止播放时，会回调这个方法
    
    
}

- (void)player:(nonnull PLPlayer *)player codecError:(nonnull NSError *)error {
    // 当解码器发生错误时，会回调这个方法
    // 当 videotoolbox 硬解初始化或解码出错时
    // error.code 值为 PLPlayerErrorHWCodecInitFailed/PLPlayerErrorHWDecodeFailed
    // 播发器也将自动切换成软解，继续播放
}


- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
        //数据源
        _dataSource = [NSMutableArray arrayWithObjects:
                       @"CCTV-1,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv1",
                       @"CCTV-2,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv2",
                       @"CCTV-3,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv3",
                       @"CCTV-4,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv4",
                       @"CCTV-6,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv6",
                       @"CCTV-7,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv7",
                       @"CCTV-8,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv8",
                       @"CCTV-9,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv9",
                       @"CCTV-10,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv10",
                       @"CCTV-11,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv11",
                       @"CCTV-12,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv12",
                       @"CCTV-13,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv13",
                       @"CCTV-14,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv14",
                       @"CCTV-15,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=cctv15",
                       @"湖南卫视,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=hnws",
                       @"北京卫视,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=bjws",
                       @"江苏卫视,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=jsws",
                       @"天津卫视,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=tjws",
                       @"东方卫视,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=dfws",
                       @"江西卫视,http://www.anchoredinversion.com/tv/ds/wasu10.php?id=jxws",
                       @"频道10,http://123.108.164.75/etv2sb/pld10499/playlist.m3u8",
                       @"频道11,http://123.108.164.75/etv2sb/pld10501/playlist.m3u8",
                       @"频道15,http://123.108.164.75/etv2sb/pld10771/playlist.m3u8",
                       @"频道16,http://123.108.164.75/etv2sb/pld10772/playlist.m3u8",
                       @"频道19,http://edge2.everyon.tv/etv2sb/pld10062/playlist.m3u8",
                       @"频道20,http://edge2.everyon.tv/etv2sb/pld10499/playlist.m3u8",
                       @"频道21,http://edge2.everyon.tv/etv2sb/pld10501/playlist.m3u8",
                       @"频道22,http://edge1.everyon.tv/etv1sb/pld10765/playlist.m3u8",
                       @"频道23,http://edge1.everyon.tv/etv1sb/pld10766/playlist.m3u8",
                       @"频道24,http://edge2.everyon.tv/etv2sb/pld10769/playlist.m3u8",
                       @"频道25,http://edge2.everyon.tv/etv2sb/pld10771/playlist.m3u8",
                       @"频道26,http://edge2.everyon.tv/etv2sb/pld10772/playlist.m3u8",
                       @"频道27,http://edge1.everyon.tv/etv1sb/pld10927/playlist.m3u8",
                       @"频道28,http://edge1.everyon.tv/etv1sb/pld10928/playlist.m3u8",
                       @"频道29,http://123.108.164.75/etv2sb/phd10062/playlist.m3u8",
                       @"频道31,http://123.108.164.75/etv2sb/phd10501/playlist.m3u8",
                       @"频道34,http://123.108.164.75/etv2sb/phd10769/playlist.m3u8",
                       @"频道35,http://123.108.164.75/etv2sb/phd10771/playlist.m3u8",
                       @"频道36,http://123.108.164.75/etv2sb/phd10772/playlist.m3u8",
                       @"澳丫卫视,http://live.mastvnet.com/n1rtlHG/500/live.m3u8",
                       @"1080P播不停,http://live.gslb.letv.com/gslb?stream_id=lb_1080P_1080p3m&tag=live&ext=m3u8&sign=live_tv&platid=10&splatid=1009&format=letv&expect=1",
                       @"动作电影,http://live.gslb.letv.com/gslb?stream_id=lb_dzdy_1300&tag=live&ext=m3u8&sign=live_tv&platid=10&splatid=1009&format=letv&expect=3",
                       @"香港电影,http://live.gslb.letv.com/gslb?stream_id=lb_hkmovie_1300&tag=live&ext=m3u8&sign=live_tv&platid=10&splatid=1009&format=letv&expect=3",
                       @"喜剧电影,http://live.gslb.letv.com/gslb?stream_id=lb_comedy_1300&tag=live&ext=m3u8&sign=live_tv&platid=10&splatid=1009&format=letv&expect=3",
                       @"日韩电影,http://live.gslb.letv.com/gslb?stream_id=lb_rhdy_800&tag=live&ext=m3u8&sign=live_tv&platid=10&splatid=1007&format=cs1&expect=3",
                       @"好莱坞高清影院 http://live.g3proxy.lecloud.com/gslb?stream_id=lb_hlw_1800&tag=live&ext=m3u8&sign=live_tv&platid=10&splatid=1009&format=letv&expect",
                       @"三立都会台,http://bcoveliveios-i.akamaihd.net/hls/live/226707/4338955585001/master.m3u8",
                       @"香港HKS,http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8",
                       @"厦门卫视,http://tm.ws.live.cntv.cn/cache/524_/seg0/index.m3u8",
                       @"厦门新闻,http://tm.ws.live.cntv.cn/cache/529_/seg0/index.m3u8",
                       @"厦门纪实,http://tm.ws.live.cntv.cn/cache/539_/seg0/index.m3u8",
                       @"厦门生活,http://tm.ws.live.cntv.cn/cache/544_/seg0/index.m3u8",
                       @"厦门影视,http://tm.ws.live.cntv.cn/cache/1059_/seg0/index.m3u8",
                       @"厦门移动,http://tm.ws.live.cntv.cn/cache/534_/seg0/index.m3u8",
                       nil];
        
        
        
        
    }
    return _dataSource;
}

@end
